﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Web.UI.DataVisualization.Charting;

namespace Site.Secure
{
    public partial class ChartView : System.Web.UI.Page
    {
        int i = 0;
        static string PrimaryBarcode;
        string[] Colors = { "Red", "Green", "Blue", "Orange" };
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /*SqlDataSource1.SelectCommand = "SELECT DISTINCT Barcode FROM DataTable";
                DataView View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
                DropDownList1.Items.Clear();
                for (int x = 0; x < View.Count; x++)
                {
                    DropDownList1.Items.Add(View[x]["Barcode"].ToString());
                    DropDownList3.Items.Add(View[x]["Barcode"].ToString());
                }*/
                PopulateChart("SELECT DISTINCT Barcode FROM DataTable");
            }
            
            

        }

        System.Drawing.Color NextColor()
        {
            if (i == Colors.Length)
                i = 0;

            System.Drawing.Color Color = System.Drawing.Color.FromName(Colors[i]);
            i++;

            return Color;
        }

        private void PopulateChart(string Statment)
        {
            Random rand = new Random();
            DataSourceSelectArguments Select = new DataSourceSelectArguments();
            SqlDataSource1.SelectCommand = Statment;
            DataView View = (DataView)SqlDataSource1.Select(Select);
            Chart1.Series.Clear();
            int x = 0;
            //Chart1.Legends.Add("Test");
            foreach (DataRowView DR in View)
            {

                SqlDataSource1.SelectCommand = "SELECT * FROM DataTable Where Barcode='" + DR["Barcode"] + "' ORDER BY Date ASC";
                DataView dv = (DataView)SqlDataSource1.Select(Select);

                Series series = Chart1.Series.Add(DR["Barcode"].ToString());
                series.Color = NextColor();
                series.ChartType = SeriesChartType.Spline;
                series.MarkerStyle = MarkerStyle.Circle;
                series.ChartArea = Chart1.ChartAreas[0].Name;

                foreach (DataRowView drv in dv)
                {
                    series.Points.AddXY(DateTime.Parse(drv["Date"].ToString()).ToShortDateString(), drv[DropDownList2.Text]);
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string BArcode = DropDownList1.Text;
            PrimaryBarcode = BArcode;
            SqlDataSource1.SelectCommand = "SELECT DISTINCT Barcode FROM DataTable";
            DataView View1 = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            DropDownList1.Items.Clear();
            for (int x = 0; x < View1.Count; x++)
            {
                DropDownList1.Items.Add(View1[x]["Barcode"].ToString());
            }

            Series s = Chart1.Series.Add(BArcode);

            s.Points.Clear();
            //Chart1.Series[0].YValueMembers = DropDownList2.Text;
            //Chart1.Series[0].XValueMember = "Date";
            s.ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Bar;
            s.ChartArea = Chart1.ChartAreas[0].Name;
            
            SqlDataSource1.SelectCommand = "SELECT DISTINCT Date, Feed FROM DataTable WHERE [Barcode]='" + BArcode + "' ORDER BY Date ASC";
            s.YValueMembers = DropDownList2.Text;
            s.XValueMember = "Date";
            
            DataView View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            foreach (DataRowView rd in View)
            {
                s.Points.AddXY(DateTime.Parse(rd["Date"].ToString()).ToShortDateString(), rd[DropDownList2.Text]);
            }
            
            Chart1.Titles[0].Text = "Chart Showing " + DropDownList2.Text + " Data for Hive: " + BArcode;            
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            ListBox1.Items.Add(DropDownList3.Text);

        }

        protected void UpdatePanel1_PreRender(object sender, EventArgs e)
        {
            
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            /*
            int x = 0;
            Chart1.Series.Clear();
            Chart1.Legends.Clear();
            Series s = Chart1.Series.Add(PrimaryBarcode);
            s.Color = System.Drawing.Color.Black;
            s.ChartType = SeriesChartType.Line;
            s.MarkerStyle = MarkerStyle.Circle;
            s.ChartArea = Chart1.ChartAreas[0].Name;
            
            s.ToolTip = "You are compairing data against this data\n Hive";
            SqlDataSource1.SelectCommand = "SELECT * FROM DataTable WHERE [Barcode]='" + PrimaryBarcode + "' ORDER BY Date ASC ";
            //s.XValueMember = "Date";
            //s.YValueMembers = "Feed";
            
            DataView View = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
            foreach (DataRowView rd in View)
            {
                s.Points.AddXY(DateTime.Parse(rd["Date"].ToString()).ToShortDateString(), rd[DropDownList2.Text]);
            }

            foreach (ListItem ls in ListBox1.Items)
            {
                Series series = Chart1.Series.Add(ls.Text);
                series.Color = System.Drawing.Color.FromName(Colors[x]);
                series.ChartType = SeriesChartType.Line;
                series.MarkerStyle = MarkerStyle.Circle;
                series.ChartArea = Chart1.ChartAreas[0].Name;
                Chart1.Legends.Add(ls.Text);



                SqlDataSource1.SelectCommand = "SELECT DISTINCT Date, Feed FROM DataTable WHERE [Barcode]='" + ls.Text + "' ORDER BY Date ASC";
                
                DataView dvView = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());
                foreach (DataRowView rd in dvView)
                {
                    series.Points.AddXY(DateTime.Parse(rd["Date"].ToString()).ToShortDateString(), rd[DropDownList2.Text]);
                }
                x++;
            }
             * */
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            PopulateChart("SELECT DISTINCT Barcode FROM DataTable WHERE " + DropDownList1.Text + "='" + TextBox1.Text + "'");
        }
    }
}